package Daily;

import java.util.HashMap;
import java.util.Map;

public class p2287重排字符形成目标字符串 {
    class Solution {
        public int rearrangeCharacters(String s, String target) {
            HashMap<Character, Integer> map = new HashMap<>();
            HashMap<Character, Integer> targetMap = new HashMap<>();
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                targetMap.put(ch, targetMap.getOrDefault(ch, 0) + 1);
            }
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                if (targetMap.containsKey(ch)){
                    map.put(ch, map.getOrDefault(ch, 0) + 1);
                }
            }
            int res = Integer.MAX_VALUE;
            for (Map.Entry<Character, Integer> entry : targetMap.entrySet()) {
                char ch = entry.getKey();
                int count = entry.getValue();
                int totalCount = map.getOrDefault(ch, 0);
                res = Math.min(res, totalCount / count);
                if (res == 0){
                    return 0;
                }
            }
            return res;
        }
    }
}
